<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Module: Spec::Rails::Mocks</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Module</strong></td>
          <td class="class-name-in-header">Spec::Rails::Mocks</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../../files/vendor/plugins/rspec-rails/lib/spec/rails/mocks_rb.html">
                vendor/plugins/rspec-rails/lib/spec/rails/mocks.rb
                </a>
        <br />
            </td>
        </tr>

        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">



   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M000017">mock_model</a>&nbsp;&nbsp;
      <a href="#M000018">stub_model</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">

    <div id="class-list">
      <h3 class="section-bar">Classes and Modules</h3>

      Module <a href="Mocks/ModelStubber.html" class="link">Spec::Rails::Mocks::ModelStubber</a><br />

    </div>




      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M000017" class="method-detail">
        <a name="M000017"></a>

        <div class="method-heading">
          <a href="#M000017" class="method-signature">
          <span class="method-name">mock_model</span><span class="method-args">(model_class, options_and_stubs = {}) {|m if block_given?| ...}</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Creates a mock object instance for a <tt>model_class</tt> with common
methods stubbed out. Additional methods may be easily stubbed (via
add_stubs) if <tt>stubs</tt> is passed.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000017-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000017-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/plugins/rspec-rails/lib/spec/rails/mocks.rb, line 11</span>
11:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">mock_model</span>(<span class="ruby-identifier">model_class</span>, <span class="ruby-identifier">options_and_stubs</span> = {})
12:         <span class="ruby-identifier">id</span> = <span class="ruby-identifier">options_and_stubs</span>[<span class="ruby-identifier">:id</span>] <span class="ruby-operator">||</span> <span class="ruby-identifier">next_id</span>
13:         <span class="ruby-identifier">options_and_stubs</span> = <span class="ruby-identifier">options_and_stubs</span>.<span class="ruby-identifier">reverse_merge</span>({
14:           <span class="ruby-identifier">:id</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">id</span>,
15:           <span class="ruby-identifier">:to_param</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">id</span>.<span class="ruby-identifier">to_s</span>,
16:           <span class="ruby-identifier">:new_record?</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword kw">false</span>,
17:           <span class="ruby-identifier">:errors</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">stub</span>(<span class="ruby-value str">&quot;errors&quot;</span>, <span class="ruby-identifier">:count</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value">0</span>)
18:         })
19:         <span class="ruby-identifier">m</span> = <span class="ruby-identifier">mock</span>(<span class="ruby-node">&quot;#{model_class.name}_#{id}&quot;</span>, <span class="ruby-identifier">options_and_stubs</span>)
20:         <span class="ruby-identifier">m</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">:__mock_proxy</span>).<span class="ruby-identifier">instance_eval</span> <span class="ruby-value str">&quot;def @target.as_new_record\nself.stub!(:id).and_return nil\nself.stub!(:to_param).and_return nil\nself.stub!(:new_record?).and_return true\nself\nend\ndef @target.is_a?(other)\n\#{model_class}.ancestors.include?(other)\nend\ndef @target.kind_of?(other)\n\#{model_class}.ancestors.include?(other)\nend\ndef @target.instance_of?(other)\nother == \#{model_class}\nend\ndef @target.class\n\#{model_class}\nend\n&quot;</span>
21:         <span class="ruby-keyword kw">yield</span> <span class="ruby-identifier">m</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">block_given?</span>
22:         <span class="ruby-identifier">m</span>
23:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000018" class="method-detail">
        <a name="M000018"></a>

        <div class="method-heading">
          <a href="#M000018" class="method-signature">
          <span class="method-name">stub_model(Model)<br />
stub_model(Model).as_new_record<br />
stub_model(Model, hash_of_stubs)<br />
stub_model(Model, instance_variable_name, hash_of_stubs)<br />
</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Creates an instance of <tt>Model</tt> that is prohibited from accessing the
database*. For each key in <tt>hash_of_stubs</tt>, if the model has a
matching attribute (determined by asking it) are simply assigned the
submitted values. If the model does not have a matching attribute, the
key/value pair is assigned as a stub return value using RSpec&#8216;s
mocking/stubbing framework.
</p>
<p>
<tt>new_record?</tt> is overridden to return the result of id.nil? This
means that by default new_record? will return false. If you want the object
to behave as a new record, sending it <tt>as_new_record</tt> will set the
id to nil. You can also explicitly set :id =&gt; nil, in which case
new_record? will return true, but using <tt>as_new_record</tt> makes the
example a bit more descriptive.
</p>
<p>
While you can use <a href="Mocks.html#M000018">stub_model</a> in any
example (model, view, controller, helper), it is especially useful in view
examples, which are inherently more state-based than interaction-based.
</p>
<h2>Database Independence</h2>
<p>
<tt><a href="Mocks.html#M000018">stub_model</a></tt> does not make your
examples entirely database-independent. It does not stop the model class
itself from loading up its columns from the database. It just prevents data
access from the object itself. To completely decouple from the database,
take a look at libraries like unit_record or NullDB.
</p>
<h2>Examples</h2>
<pre>
  stub_model(Person)
  stub_model(Person).as_new_record
  stub_model(Person, :id =&gt; 37)
  stub_model(Person) do |person|
    person.first_name = &quot;David&quot;
  end
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000018-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000018-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/plugins/rspec-rails/lib/spec/rails/mocks.rb, line 98</span>
 98:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">stub_model</span>(<span class="ruby-identifier">model_class</span>, <span class="ruby-identifier">stubs</span>={})
 99:         <span class="ruby-identifier">stubs</span> = {<span class="ruby-identifier">:id</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">next_id</span>}.<span class="ruby-identifier">merge</span>(<span class="ruby-identifier">stubs</span>)
100:         <span class="ruby-identifier">returning</span> <span class="ruby-identifier">model_class</span>.<span class="ruby-identifier">new</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">model</span><span class="ruby-operator">|</span>
101:           <span class="ruby-identifier">model</span>.<span class="ruby-identifier">id</span> = <span class="ruby-identifier">stubs</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">:id</span>)
102:           <span class="ruby-identifier">model</span>.<span class="ruby-identifier">extend</span> <span class="ruby-constant">ModelStubber</span>
103:           <span class="ruby-identifier">stubs</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">k</span>,<span class="ruby-identifier">v</span><span class="ruby-operator">|</span>
104:             <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">model</span>.<span class="ruby-identifier">has_attribute?</span>(<span class="ruby-identifier">k</span>)
105:               <span class="ruby-identifier">model</span>[<span class="ruby-identifier">k</span>] = <span class="ruby-identifier">stubs</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">k</span>)
106:             <span class="ruby-keyword kw">end</span>
107:           <span class="ruby-keyword kw">end</span>
108:           <span class="ruby-identifier">add_stubs</span>(<span class="ruby-identifier">model</span>, <span class="ruby-identifier">stubs</span>)
109:           <span class="ruby-keyword kw">yield</span> <span class="ruby-identifier">model</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">block_given?</span>
110:         <span class="ruby-keyword kw">end</span>
111:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>